<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>

<%@ page import="com.conversationboard.model.*" %>
<%@ page import="com.conversationboard.view.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.security.Principal" %>
<%@ page import="com.conversationboard.config.*" %>
<%@ page import="com.conversationboard.site.*" %>

<% 
	Site.incrementPageHitCount();
	
    Principal principal = request.getUserPrincipal();
    String userName = null;
    
    if (principal != null) {
    	userName = principal.getName();
    } else {
    	
    	/* Try to automatically log you in, if you have login cookies, by forcing a redirect to the login page,
    	   which should log you in and pass you back here. */
		
    	if (AuthenticationCookie.getUserName(request) != null) {
    		response.sendRedirect(Configuration.getInstance().getRoot() + "/LoginControllerServlet?autologin=true");
    		return;
    	}
    }
   
 	User user = User.get(userName);
 	Bookmarks bookmarks = user.getBookmarks();

    /* Get the list of boards */
    List<Board> authorisedBoards = Boards.getAuthorisedBoards(user, false);
    List<Board> boards = Boards.orderBoards(authorisedBoards);

    request.setAttribute("user", user);
    request.setAttribute("boards", boards);
    request.setAttribute("numberOfUsers", Site.getUserCount());
    request.setAttribute("numberOfThreads", Site.getThreadCount());
    request.setAttribute("numberOfMessages", Site.getMessageCount());
    
    request.setAttribute("config", Configuration.getInstance());
    
%>

<%@page import="com.conversationboard.authenticator.AuthenticationCookie"%>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta name="ROBOTS" content="NOINDEX"></meta>
	<title><%=Configuration.getInstance().getSiteName()%></title>
	<link rel="stylesheet"title="Conversation Board" type="text/css" href="${contextPath}/Stylesheets/<%=StyleSheets.getStyleSheetFileName(request.getCookies())%>">
</head>

<body>

	<%= MainMenu.display(principal) %>
	
	<% if (SiteMessage.getMessage() != null) { %>	
		<div class='sitemessage'><%= SiteMessage.getMessage() %></div>
	<% } %>

	<% if (BatchMessage.getMessage() != null) { %>	
		<div class='batchmessage'><%= BatchMessage.getMessage() %></div>
	<% } %>
	
	<% if (user.isSiteAdmin() && (Site.getPendingRegistrations() > 0)) { %>
		<div class='batchmessage'><%= Site.getPendingRegistrations() %> pending registration(s).</div>
	<% } %>

	<%= BookmarksDisplay.display(bookmarks, false) %>

	<c:if test="${siteshut}">
		<div class="sitemessage"><strong><span class="red">Site is currently shut. You are accessing as an administrator. Open the site in the admin menu option <a href="${contextPath}/Pages/Admin/openclose.jsp">Open/Close Site</a></span></strong></div>
	</c:if>
	
	<div class="headingblock">
		<h1><%= Configuration.getInstance().getSiteName()%>
		<% if (Configuration.getInstance().isTestServer()) { %>
			(Test Server)
		<% } %>
		</h1>
	</div>

	<div class="mainblock">

		<p><a href="Pages/help.jsp">I'm new to <%= Configuration.getInstance().getSiteName()%>: what is it all about?</a></p>
	
		<table class="table">

			<tr class="header">
				<td class="cell">Board Name</td>
				<td class="cell">Board Description</td>
				<td class="cell">Board Type</td>
				
				<c:if test="${user.siteAdmin}">
					<td class="cell">Administration</td>
				</c:if>
				
			</tr>

		    <c:forEach var="board" items="${boards}">
				<tr class="row">

					<c:choose>
						<c:when test="${board.popularBoard}">
							<td class="cell"><strong><a href="NewAnswersControllerServlet?boardid=${board.boardId}">${board.name}</a></strong></td>
						</c:when>
						<c:otherwise>
							<td class="cell"><a href="NewAnswersControllerServlet?boardid=${board.boardId}">${board.name}</a></td>
						</c:otherwise>
					</c:choose>

					<td class="cell">${board.description}</td>
					
					<c:choose>
						<c:when test="${board.privateBoard}">
							<td class="cell"><span class="red">Private</span></td>
						</c:when>
						<c:when test="${board.inviteOnly}">
							<td class="cell"><span class="amber">Invite Only</span></td>
						</c:when>
						<c:otherwise>
							<td class="cell"><span class="green">Public</span></td>
						</c:otherwise>
					</c:choose>

					<c:if test="${user.siteAdmin}">
						<td class="cell">
								<a href="Pages/Admin/editboard.jsp?boardid=${board.boardId}">Edit...</a>
						</td>
					</c:if>

				</tr>
		    </c:forEach>

		</table>

		<p>
			<c:if test="${user.siteAdmin or (user.loggedIn and config.allowUserBoardCreation)}">
				<a class="leftmenulink" href="Pages/createboard.jsp">Create a New Board</a>
			</c:if>
	
			<a class="menulink" href="${contextPath}/Pages/sitemap.jsp">View Site Map</a>
			<a class="menulink" href="${contextPath}/RandomThreadsControllerServlet">Random Threads</a>
			<!-- <a class="menulink" href="${contextPath}/Pages/most-read-threads.jsp">Most Read Threads</a>-->
	
			<c:if test="${config.allowUserBoardCreation and not user.loggedIn}">
				<p><strong>If you wish to create a new board, you must first be registered and logged in.</strong></p>
			</c:if>
		</p>
	
		<div class="adminmenulinks">
			<c:if test="${user.admin}">
				<a href="Pages/Admin/adminmenu.jsp">Administration Menu</a><br />
				<a href="<%=Configuration.getInstance().getRoot()%>/PerformanceStatsControllerServlet">Performance Statistics</a><br />
				<a href="<%=Configuration.getInstance().getRoot()%>/RecentActiveUsersControllerServlet">Recently Active Users</a><br />
			</c:if>
			<c:if test="${(user.loggedIn and config.displayAdminLogToUsers) or (user.siteAdmin and config.displayAdminLogToSiteAdmins)}">
					<a href="${contextPath}/ViewAdminLogControllerServlet?pageNumber=0">Admin Log</a><br />
			</c:if>
		</div>

		<p>
			<span class="menulink"><strong><fmt:formatNumber groupingUsed="true">${numberOfUsers}</fmt:formatNumber></strong>  registered users</span>
			<span class="menulink"><strong><fmt:formatNumber groupingUsed="true">${numberOfThreads}</fmt:formatNumber></strong> threads</span>
			<span class="menulink"><strong><fmt:formatNumber groupingUsed="true">${numberOfMessages}</fmt:formatNumber></strong> messages</span>
			<span class="menulink">(figures updated daily)</span>
		</p>

	</div>
		
	<!--<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>-->
	<!--<script type="text/javascript">_uacct = "UA-1148679-1";urchinTracker();</script>--> 

</body>
</html>
